package com.arcvideo.acsconnect.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arcvideo.acsconnect.app.AcsConnectApplication;
import com.arcvideo.acsconnect.rest.model.UserBean;
import com.arcvideo.acsconnect.ui.MainActivity;
import com.arcvideo.tyingyitonginhouse.R;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2018/7/12
 * @description
 */

public class UserChangeDialog extends Dialog {

    @BindView(R.id.iv_user1)
    ImageView mIvUser1;
    @BindView(R.id.tv_username1)
    TextView mTvUsername1;
    @BindView(R.id.tv_user_id1)
    TextView mTvUserId1;
    @BindView(R.id.iv_user2)
    ImageView mIvUser2;
    @BindView(R.id.tv_username2)
    TextView mTvUsername2;
    @BindView(R.id.tv_user_id2)
    TextView mTvUserId2;
    @BindView(R.id.ll_root)
    LinearLayout mLlRoot;
    @BindView(R.id.ll_user_1)
    LinearLayout mLlUser1;
    @BindView(R.id.ll_user_2)
    LinearLayout mLlUser2;


    private Context mContext;
    private MainActivity mActivity;

    public UserChangeDialog(@NonNull MainActivity activity) {
        super(activity, R.style.Dialog);
        this.mContext = activity;
        this.mActivity = activity;
        init();
        initView();
        initEvent();
    }

//    public void setActivity(MainActivity activity) {
//        this.mActivity = activity;
//    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_user_change, null, false);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        addContentView(view, layoutParams);

        Window window = this.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = (int) (d.getWidth() * 0.2);
        p.height = (int) (d.getWidth() * 1);
        p.gravity = Gravity.LEFT;
        window.setAttributes(p);
        window.setBackgroundDrawable(null);//去掉默认的边距
        window.setWindowAnimations(R.style.UserChangeDialogAnim);
        this.getWindow().getDecorView().setPadding(0, 0, 0, 0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
//                ToastUtil.showToast("down");
//                mIvUser2.requestFocus();
                break;

            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_VOLUME_UP:
//                ToastUtil.showToast("up");
//                mIvUser1.requestFocus();
                break;

            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:
                int currentIndex = AcsConnectApplication.sCurrentUserIndex;
                if ((mLlUser1.hasFocus() && currentIndex == 0) || (mLlUser2.hasFocus() && currentIndex == 1)) {
                    dismiss();
                } else {
                    mActivity.changeUser(mLlUser1.hasFocus() ? 0 : 1);
                    dismiss();
                }
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_BACK:
                dismiss();
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initView() {

        LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) mLlRoot.getLayoutParams();
        ll.height = mActivity.getWindowManager().getDefaultDisplay().getHeight();
        ll.width = mActivity.getWindowManager().getDefaultDisplay().getWidth() / 5;
        mLlRoot.setLayoutParams(ll);

        List<UserBean> list = AcsConnectApplication.sUserList;
        UserBean userBeanFirst = null;
        UserBean userBeanSecond = null;
        if (null != list && list.size() > 0) {
            userBeanFirst = list.get(0);
            Glide.with(mContext).load(userBeanFirst.getHeadPicUrl()).into(mIvUser1);
            mTvUsername1.setText(userBeanFirst.getName());
            mTvUserId1.setText(userBeanFirst.getAccount());
        }
        if (list.size() > 1) {
            userBeanSecond = list.get(1);
            Glide.with(mContext).load(userBeanSecond.getHeadPicUrl()).into(mIvUser2);
            mTvUsername2.setText(userBeanSecond.getName());
            mTvUserId2.setText(userBeanSecond.getAccount());
        }else{
            mLlUser2.setVisibility(View.GONE);
        }
        if (AcsConnectApplication.sCurrentUserIndex == 0) {
            mLlUser1.requestFocus();
        } else {
            mLlUser2.requestFocus();
        }
    }

    private void initEvent() {
    }


}
